package com.ewanghuiju.app.ui.main.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.main.WelcomeContract;
import com.ewanghuiju.app.model.bean.response.WelcomeBean;
import com.ewanghuiju.app.model.prefs.ImplPreferencesHelper;
import com.ewanghuiju.app.presenter.main.WelcomePresenter;
import com.ewanghuiju.app.ui.home.activity.SearchShopResultActivity;
import com.ewanghuiju.app.ui.login.activity.LoginMainActivity;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanShopDetailsActivity;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.ClipboardUtils;
import com.ewanghuiju.app.util.PermissionUtil;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.widget.dialog.XieyiDialog;
import com.muddzdev.styleabletoast.StyleableToast;

import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/11
 */
public class WelcomeActivity extends BaseActivity<WelcomePresenter> implements WelcomeContract.View {

    @BindView(R.id.iv_welcome_bg)
    ImageView ivWelcomeBg;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initEventAndData() {
//        ImageLoader.load(mContext,R.mipmap.bg_splash,ivWelcomeBg);
        finishAnim = false;
//        mPresenter.getWelcomeData();
        ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
        boolean isAccepAgreement = mImplPreferencesHelper.getIsAccepAgreement();
        if (isAccepAgreement) {
            //同意了隐私协议
            getPermission();
        } else {
            //未同意  弹出框
            showAccep();
        }
    }

    @Override
    public void showContent(WelcomeBean welcomeBean) {
//        ImageLoader.load(this, welcomeBean.getImg(), ivWelcomeBg);
//        ivWelcomeBg.animate().scaleX(1.12f).scaleY(1.12f).setDuration(2000).setStartDelay(1000).start();
    }


    @Override
    public void jumpToMain() {
        Intent intent = new Intent(mContext, MainActivity.class);
        startActivity(intent);
        onBackPressedSupport();
    }

    @Override
    public void showAccep() {
//        if (FloatWindowManager.getInstance().checkPermission(mContext)) {
//        mContext.getParent()
        XieyiDialog xieyiDialog = new XieyiDialog(mContext, new XieyiDialog.ClockResult() {
            @Override
            public void onNo() {
                //退出app
                App.getInstance().exitApp();
            }

            @Override
            public void onYes() {
                try {
                    ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
                    mImplPreferencesHelper.setIsAccepAgreement(true);
                    getPermission();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onLookxieyi() {
                //查看协议
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "用户协议与隐私政策").startActivity(true);
            }
        });
        xieyiDialog.show();
        xieyiDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    //back键
                    return true;
                } else {
                    return false; //默认返回 false
                }
            }
        });
//        PrivateXyPopup customBasePopup = new PrivateXyPopup(mContext,
//                new PrivateXyPopup.XyPopupWindowCallback() {
//                    @Override
//                    public void sureClickCallback() {
//                        try {
//                            ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
//                            mImplPreferencesHelper.setIsAccepAgreement(true);
//                            getPermission();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                    @Override
//                    public void cancelClickCallback() {
//                        //退出app
//                        App.getInstance().exitApp();
//                    }
//
//                    @Override
//                    public void lookxyCallback() {
//                        //查看协议
//                        new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
//                                .putExtra(Constants.WEBURL_TITLE, "用户协议与隐私政策").startActivity(true);
//                    }
//                });
//        customBasePopup.setWidth(customBasePopup.getScreenWidth() * 2 / 3);
//        customBasePopup.setPopupGravity(Gravity.CENTER);
//        customBasePopup.setOutSideDismiss(false);
//        customBasePopup.setBackPressEnable(false);
//        customBasePopup.showPopupWindow();
    }

    /**
     * 读取权限信息
     */
    @Override
    public void getPermission() {
        PermissionUtil.requestPermissionForActivity(mContext, new PermissionUtil.IPermissionListener() {
            @Override
            public void permissionGranted() {
                toGuideOrMain();
            }

            @Override
            public void permissionDenied() {
                StyleableToast.makeText(mContext, "权限被拒绝,请设置应用权限", Toast.LENGTH_SHORT, R.style.mytoast).show();
                toGuideOrMain();
            }
        }, "权限被拒绝,请设置应用权限", PermissionUtil.home_permissions);
    }

    public void toGuideOrMain() {
        ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
        boolean isFirst = mImplPreferencesHelper.getIsFirst();
        if (isFirst) {
            String mToken = mImplPreferencesHelper.getToken();
            if (TextUtils.isEmpty(mToken)) {
                new StartActivityUtil(mContext, LoginMainActivity.class).startActivity(false);
            } else {
                Intent intent = getIntent();
                Uri uri = intent.getData();
                if (uri != null) {
                    String mType = uri.getQueryParameter("mw_cp_type");
                    if (!TextUtils.isEmpty(mType)) {
                        ClipboardUtils.copyText("", "");
                        if (mType.equals("1")) {
                            //商品详情
                            String goodsId = uri.getQueryParameter("mw_cp_goods_id");
//                            if (!TextUtils.isEmpty(goodsId)) {
                                try {
                                    new StartActivityUtil(mContext, MainActivity.class).putExtra(Constants.TO_MAIN_TYPE,2)
                                            .putExtra(Constants.TO_MAIN_VALUE,goodsId)
                                            .startActivity(false);
                                } catch (NumberFormatException e) {
                                    e.printStackTrace();
                                }
//                            }else{
//                                new StartActivityUtil(mContext, MainActivity.class).putExtra(Constants.TO_MAIN_TYPE,1).startActivity(false);
//                            }
                        } else if (mType.equals("2")) {
                            //拼团详情
                            String group_no = uri.getQueryParameter("mw_cp_group_no");
//                            if (!TextUtils.isEmpty(group_no)) {
                                new StartActivityUtil(mContext, MainActivity.class).putExtra(Constants.TO_MAIN_TYPE,3)
                                        .putExtra(Constants.TO_MAIN_VALUE,group_no).startActivity(false);
//                            }else{
//                                new StartActivityUtil(mContext, MainActivity.class).putExtra(Constants.TO_MAIN_TYPE,1).startActivity(false);
//                            }
                        } else{
                            new StartActivityUtil(mContext, MainActivity.class).putExtra(Constants.TO_MAIN_TYPE,1).startActivity(false);
                        }
                    }
                    else{
                        new StartActivityUtil(mContext, MainActivity.class).putExtra(Constants.TO_MAIN_TYPE,1).startActivity(false);
                    }
                }else{
                    new StartActivityUtil(mContext, MainActivity.class).putExtra(Constants.TO_MAIN_TYPE,1).startActivity(false);
                }
            }
        } else {
            new StartActivityUtil(mContext, GuideActivity.class).startActivity(false);
        }
        onBackPressedSupport();
    }

    @Override
    protected void initImmersionBar() {
//        super.initImmersionBar();
//        ImmersionBar.with(this).fullScreen(true)
//                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
